<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'document-form',
	'enableAjaxValidation'=>false,
  'htmlOptions'=>array('class'=>'form-horizontal','enctype' => 'multipart/form-data'),
)); ?>

	<p class="note">Những trường đánh dấu <span class="required">*</span> là trường bắt buộc.</p>

	<?php echo $form->errorSummary($model,'','',array('class'=>'alert alert-error')); ?>

	<div class="control-group">
		<?php echo $form->labelEx($model,'name',array('class'=>'control-label')); ?>
		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255)); ?>
	</div>
	
	<div class="control-group">
		<?php echo $form->labelEx($model,'subjectID',array('class'=>'control-label')); ?>
		<?php echo $form->dropDownList($model,'subjectID',Subject::model()->getListSubject(),array('onchange'=>'loadTopicAjax(this.value)')); ?>
	</div>
	
	<div class="control-group">
		<?php echo $form->labelEx($model,'topicID',array('class'=>'control-label')); ?>
		<?php echo $form->dropDownList($model,'topicID',array(),array('prompt'=>'---Chọn môn học---')); ?>
	</div>
	
	<div class="control-group">
		<?php echo $form->labelEx($model,'photo',array('class'=>'control-label')); ?>
		<?php echo $form->fileField($model,'photo'); ?>
		<div id="img_avatar">
    <?php if (!empty($img)) {?>
    <a href="javascript: void(0)" onclick="RemoveImage(<?php echo $model->ID?>)" class="icon-remove"></a>
    <img class="img-polaroid img-rounded" id="avatar" alt="avatar" src="<?php echo $img?>">
    <?php }?>
    </div>
	</div>
	
	<div class="control-group">
		<?php echo $form->labelEx($model,'description',array('class'=>'control-label')); ?>
		<?php echo $form->textArea($model,'description',array('rows'=>3, 'cols'=>50, 'max'=>500)); ?>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'content',array('class'=>'control-label')); ?>
		<div class="clearfix"></div>
		<?php
  		$this->widget('ext.ckeditor.CKEditorWidget',array(
        "model"=>$model,
        "attribute"=>'content',
      	"config" => array(
	      "toolbar"=>"Full",
				"height"=>"400px",
				'width'=>"80%",
      ),
      )); 
  	?>
	</div>

	<div class="control-group">
		<?php echo $form->labelEx($model,'enabled',array('class'=>'control-label')); ?>
		<div class="gender">
      <?php echo $form->radioButtonList($model,'enabled',array('1'=>'Hiện','0'=>'Ẩn'), array('separator'=>'','template'=>'{input}{label}')) ?>
    </div>
	</div>

	<?php echo CHtml::submitButton($model->isNewRecord ? 'Tạo mới' : 'Cập nhật', array('class'=>'btn btn-success','onclick'=>'javascript:CKupdate();')); ?>

<?php $this->endWidget(); ?>
<script type="text/javascript">
$(function() {
	loadTopicAjax($("#Document_subjectID").val());
});
function loadTopicAjax(subjectID)
{
	$.ajax({
		url: baseUrl + "/admin/document/LoadTopicAjax",
		data: {subjectID: subjectID},
		type: "POST",//or GET,
		dataType:"json",
		success: function(data) {
			if(data.result=="success") {
				$("#Document_topicID").html(data.html);
			}
		}
	});	
}
function RemoveImage(id) {
	var notice = '<?php echo "Bạn có chắc muốn xóa hình ảnh này?"?>';
	if(confirm(notice)) {
		$.ajax({
				"url": baseUrl + "/document/removeimage",
        "data":{"ID" : id},
        "type":"post",
        success:function(data){
          if(data=="success") {
          	$('#img_avatar').remove();
          }
        },
        beforeSend: function() {
        	$("#img_avatar").html("");
  			  $("#img_avatar").addClass("load");
  		  }
		});
	}
}
function CKupdate(){
  for (instance in CKEDITOR.instances)
      CKEDITOR.instances[instance].updateElement();
}
</script>